/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: WX_BaseUtil
 * Author:   xi
 * Date:     2018/5/17 14:17
 * Description: wechat
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.supconit.util;


import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 〈一句话功能简述〉<br> 
 * 〈wechat 〉
 *
 * @author xi
 * @create 2018/5/17
 * @since 1.0.0
 */
public class WeixinUtil {

    private static final Logger logger = LoggerFactory.getLogger(WeixinUtil.class);

    private static final String OAUTHUSERINFO="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";

    private static final String USERINFOPATH="https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    private static final String REFRESHPATH="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";

    private static final String TOKENPATH="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";

    private static final String USERINFOPATH2="https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    private static final String GETOPENIDPATH="https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
    /**
     * 根据Code获取openId
     * @param appId
     * @param appSecret
     * @param code
     * @return
     */
    public static JSONObject getWxInfo(String appId,String appSecret,String code){
        String path = OAUTHUSERINFO.replace("APPID", appId).replace("SECRET", appSecret).replace("CODE", code);
        JSONObject object=WX_HttpsUtil.httpsRequest(path, "GET", null);
        return object;
    }

    /**
     * 根据Code获取openId
     * @param token
     * @param openId
     * @return
     */
    public static JSONObject getOpenIds(String token,String openId){
        String path = GETOPENIDPATH.replace("ACCESS_TOKEN", token).replace("NEXT_OPENID", openId);
        logger.info(path);
        JSONObject object=WX_HttpsUtil.httpsRequest(path, "GET", null);
        logger.info(object.toString());
        return object;
    }


    public static String getToken(String appId,String appSecret){
        logger.info("APPID:" + appId+"    SECRET:"+appSecret);
        String path = TOKENPATH.replace("APPID", appId).replace("SECRET", appSecret);
        logger.info(path);
        JSONObject object=WX_HttpsUtil.httpsRequest(path, "GET", null);
        logger.info(object.toString());
        return object.getString("access_token");
    }

    public static String refreshToken(String appId,String token){
        String path = REFRESHPATH.replace("APPID", appId).replace("REFRESH_TOKEN", token);
        JSONObject object=WX_HttpsUtil.httpsRequest(path, "GET", null);
        return object.getString("access_token");
    }

    /**
     * 根据openId获取用户对象
     * @param openId
     * @return
     */
    public static JSONObject getUserInfo(String openId,String token){
        String path = USERINFOPATH.replace("OPENID", openId).replace("ACCESS_TOKEN",token);
        JSONObject object=WX_HttpsUtil.httpsRequest(path, "GET", null);
        return object;
    }

    /**
     * 根据openId获取用户对象
     * @param openId
     * @return
     */
    public static JSONObject getUserInfo2(String openId,String token){
        String path = USERINFOPATH2.replace("OPENID", openId).replace("ACCESS_TOKEN",token);
        logger.info(path);
        JSONObject object=WX_HttpsUtil.httpsRequest(path, "GET", null);
        return object;
    }

    /**
     * 获取系统中的UserId
     * @return
     */
    public static String getUserId(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //查询cookie
        Cookie cookie = CookieUtil.get(request, CookieConstant.USERID);
        // TODO 测试用
        if(cookie==null){
            return "000000";
        }

        return cookie.getValue();
    }

    /**
     * 获取系统中的UserId
     * @return
     */
    public static String getUserNm(String openId){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //查询cookie
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
       // TODO 测试用
        if("000000".equals(openId)){
            return "测试人员";
        }
        if(cookie==null){
            return "匿名用户";
        }
        String token =cookie.getValue();
        JSONObject obj = getUserInfo(openId,token);
        // 根据Id获取用户信息
        return obj.getString("nickname");
    }
}